Ext.define('GBIF.AdminGrid', {
		extend: 'Ext.grid.Panel'	
	,	alias: 'widget.admingrid'
	,	border: false
	,	autoScroll: true
	,	viewConfig: {
				stripeRows: false
			,	emptyText: '<div style="padding:10px;">No project to display.</div>'
        }
	,	loadMask: true
	,	columnLines: true
	,	enableColumnHide: false
	,	listeners: {
			itemcontextmenu: function(grid, record, item, index, e){
				var items = [];
				if(!Ext.isEmpty(record.data.url) && Ext.isDefined(record.data.url)){
					items.push({
							text: 'Open tab to DwC Archive'
						,	iconCls: 'icon_link_go'
						,	scope: this	
						,	handler: function(me) {
								window.open(record.data.url, '_blank');
							}  
					});
				}
				items.push({
						text: 'Remove Project'
					,	iconCls: 'icon_delete'
					,	scope: this	
					,	handler: function(me) {
							this.removeRequest(record);
						}  
				});
				var menu = Ext.create('Ext.menu.Menu', {
						items: items
				});
				var xy = e.getXY();
				menu.showAt(xy);
			}
		}
	,	initComponent: function() {	
			this.store = Ext.create('Ext.data.Store', {
					pageSize: 25
				,	model: 'ProjectModel'
				,	remoteSort: true
				,	autoLoad: true
				,	proxy: {
							type: 'ajax'
						,	url: 'resources/api/app.php'
						,	extraParams: {
									cmd: 'listProjects'
								,	projectId: ''
							}
						,	reader: {
									type: 'json'
								,	root: 'records'
								,	totalProperty: 'recordCount'
							}
						,	simpleSortMode: true
					}
				,	sorters: [{
							property: 'dateAdded'
						,	direction: 'DESC'
					}]	
			});
			
			
			this.columns = [
					{header: "Project Code", width: 80, sortable: true, dataIndex: 'projectId', draggable: false, renderer: function(v) {
						return('<a target="_blank" href="../?nodebug&projectID=' + v + '">' + v + '</a>');
					}}
				,	{header: "Date Created", width: 125, sortable: true, dataIndex: 'dateAdded', draggable: false}
				,	{header: "Current Stage", width: 80, sortable: true, dataIndex: 'stage', draggable: false}
				,	{header: "# of Rows", width: 80, sortable: true, dataIndex: 'recordCount', draggable: false}
				,	{header: "Disk Space", width: 80, sortable: true, dataIndex: 'diskSize', draggable: false, renderer: this.getDiskSizeInMB}
				,	{header: "Url", width: 300, sortable: true, dataIndex: 'url', draggable: false}
			];
			this.callParent();
		}
	,	getDiskSizeInMB: function(value){
				return(Math.round(value/1048576*100000)/100000 + " MB");
		}	
	,	removeRequest: function(record){
			var projectId = record.data.projectId;
			var str = Ext.util.Format.format('Are you sure delete "{0}" project ?', projectId);
					Ext.Msg.confirm('Notice', str, function(btn){
							if(btn == 'yes'){
								Ext.Ajax.request({
										url : 'resources/api/app.php'
									,	params: {
												cmd: 'deleteProject'
											,	projectId: projectId	
										}
									,	scope: this
									,	success: function (response) {
											var o = Ext.decode(response.responseText);
											if(o.success){
													this.store.load();
													GBIF.Notice.msg('Success', projectId + ' Removed Successfully.');
											}else {
													GBIF.Notice.msg('Error', o.error.msg);
											}
										}
									,	failure: function () {
												GBIF.Notice.msg('Error', 'Error in connection.');
										}
								});
							}
					}, this);
			
		}	
});